import wybTable from '@/components/wyb-table/wyb-table.vue'
import member from '@/components/selectMember/selectMember.vue'
import {getUrlParam,filterData2} from '@/static/js/filter.js'
export default {
	components: {
		wybTable,
		member
	},
	data() {
		const currentDate = this.getDate({
			format: true
		})
		return {
			userName:'',
			examinationOrgan:'',
			abnormalName:'',
			data:{
				reportItemS:[]
			},
			headers: [{
				label: '名称',
				key:'name',
				width: 200,
			}, {
				label: '结果',
				key:'input',
				width: 150,
			}, {
				label: '参考值',
				key:'va',
				width: 200,
			}, {
				label: '单位',
				key:'units',
				width: 200,
			}],
			defaultData:[], //基础体检数据
			dialog:false,
			dialogcontents: [],
			dialogHeaders:[
				{
					label: '异常报告',
					key:'name',
					Wpercentage: '82vw'
				}
			],
			// 异常体检表头
			dialogHeaders2:[
				{
					label: '科室',
					key:'sectionName'
				},
				{
					label: '名称',
					key:'name'
				},
				{
					label: '参考范围',
					key:'va'
				},
				{
					label: '单位',
					key:'units'
				}
			],
			// 异常体检表头
			dialogHeaders3:[
				{
					label: '名称',
					key:'name',
					width: 200,
				},
				{
					label: '结果',
					key:'input',
					width: 150,
				},
				{
					label: '参考范围',
					key:'va',
					width: 200,
				},
				{
					label: '单位',
					key:'units',
					width: 200,
				}
			],
			isCheckYc:[],
			isCheckYcData:[], //选中的异常数据
			isCheckDx:[],
			isCheckDxData:[], //选中的单项数据
			beforeFilterData:[], //转换前的数据
			pageIndex: 1,
			totalCount:0,
			// 所有体检项目分页参数
			tiOption:{
				pageIndex:1,
				totalCount:0,
			},
			dxgetcallback:[],
			Type:'add',
			mid:'',
			medicalStatus:'',
			sex: '',
			start: '',
			end: currentDate,
			start_date:'',
			memberdialog: false,
			editId: '',
			showModal:false,
			showContent:'',
			isChoose: true,
			status: 1 ,//存档状态
			entrance:'',
			reportStatus:''
		}
	},
	mounted() {
		this.getReportTemplateStencilAxios()
		if(getUrlParam('entrance')){
			// 从会员申请列表进入的
			this.entrance = getUrlParam('entrance')
			this.userName = decodeURI(getUrlParam('userName'))
			this.userId = getUrlParam('userId')
			this.reportNumber = getUrlParam('reportNumber')
			this.reportStatus = getUrlParam('reportStatus')
			this.sex = decodeURI(getUrlParam('sex'))
			this.getfindByMedicalAxios()
		}else{
			if(getUrlParam('reportNo') && getUrlParam('userId')){
				this.status = getUrlParam('status')
				if(getUrlParam('status') == 2){
					uni.setNavigationBarTitle({
						title: '查看体检报告'
					})
				}
				this.editId = getUrlParam('id')
				this.userName = decodeURI(getUrlParam('userName'))
				this.userId = getUrlParam('userId')
				this.reportNo = getUrlParam('reportNo')
				this.sex = getUrlParam('sex') == 2?'女':'男'
				this.Type = 'edit'
				this.getfindByMedicalAxios()
			}else{
				this.status = 1
				this.Type = 'add'
			}
		}
	},
	methods: {
		// 日期
		bindDateChange(e) {  //日期选择
			this.start_date = e.target.value;
		},
		// 获取当前时间
		getDate(type) {
		   const date = new Date();
		   let year = date.getFullYear();
		   let month = date.getMonth() + 1;
		   let day = date.getDate();
		  
		   if (type === 'start') {
			year = year - 60;
		   } else if (type === 'end') {
			year = year + 2;
		   }
		   month = month > 9 ? month : '0' + month;;
		   day = day > 9 ? day : '0' + day;
		   return `${year}-${month}-${day}`;
		},
		// 查询体检报告--或体检报告详情
		getfindByMedicalAxios(){
			const _this = this;
			var bArr = []
			_this.$api.findByMedical(getUrlParam('userId'),getUrlParam('entrance')? getUrlParam('reportNumber') :  getUrlParam('reportNo')).then((res)=>{
				_this.start_date = filterData2(new Date(res.data.result.reportDate).getTime())
				_this.examinationOrgan = res.data.result.examinationOrgan
				_this.mid = res.data.result.id
				_this.defaultData = _this.defaultData.map((a,ai)=>{
					const b = a.contents.map((b,bi)=>{
						const c = res.data.result.reportItemList.map((c,ci)=>{
							// if(c.itemId)
							if(b.reportNo == c.itemId){
								b.value = c.itemValue
							}
							return c
						})
						_this.data.reportItemS.map((d,di)=>{
							if(d.itemId && d.itemId == b.reportNo){
								d.itemValue = b.value
							}
						})
						return b
					})
					bArr = bArr.concat(b)
					return a
				})
				// 单项数据回显
				const result = bArr.map((a)=>{
					return a.reportNo
				})
				var isCheckDxDataArr = []
				res.data.result.reportItemList.map((c,ci)=>{
					let itemId = c.itemId
					if(result.indexOf(c.itemId) == -1){
						c.NewitemId = itemId
						c.checked = true
						c.sectionName = c.sectionName
						c.name = c.itemName
						c.value = c.itemValue
						c.units = c.itemUnit
						c.va = Number(c.minValue) == 0 && Number(c.maxValue) == 0 ? '':  Number(c.minValue).toFixed(2)+'~'+Number(c.maxValue).toFixed(2)
						isCheckDxDataArr.push(c)
					}
				})
				_this.isCheckDxData = isCheckDxDataArr
				_this.isCheckYcData = res.data.result.reportAbnormalList &&  res.data.result.reportAbnormalList.map((aa)=>{
					aa.name = aa.abnormalName
					aa.reportNo = aa.abnormalCode
					return aa
				}) || []
				_this.Type = res.data.Result?'edit':'add'
				_this.mid = res.data.Result && res.data.Result.id
			})
		},
		// 选中的异常
		onCheck(e){
			let data = e.data.map((res)=>{return res.lineData})
			if(this.type == 'yczb'){
				this.isCheckYc = data
				console.log(data)
			}else{
				this.isCheckDx = data
			}
		},
		dialogSubmit(){
			if(this.type == 'yczb'){
				if(this.isCheckYcData.length <= 0){
					this.isCheckYcData = this.isCheckYc
				}else{
					const hasRepeat = this.isCheckYcData.filter((a,ai)=>{
						const arr = this.isCheckYc.filter((b,bi)=>{return b.name == a.name})
						return arr.length > 0
					})
					if(hasRepeat.length > 0){
						uni.showToast({title:'已有重复项',icon:'none'})
						return;
					}
					this.isCheckYcData = this.isCheckYcData.concat(this.isCheckYc)
				}
				this.dialog = false;
			}else{
				const isrepeat = []
				this.defaultData.map((a)=>{
					a.data.map((b)=>{
						this.isCheckDx.map((c)=>{
							if(c.itemName == b.itemName){
								isrepeat.push(c)
							}
						})
					})
				})
				if(isrepeat.length > 0){
					uni.showToast({title:isrepeat[0].itemName+'重复',icon:'none'})
					return;
				}
				if(this.isCheckDxData.length <= 0){
					this.isCheckDxData = this.isCheckDx
				}else{
					const hasRepeat = this.isCheckDxData.filter((a,ai)=>{
						const arr = this.isCheckDx.filter((b,bi)=>{return b.name == a.name})
						return arr.length > 0
					})
					if(hasRepeat.length > 0){
						uni.showToast({title:'已有重复项',icon:'none'})
						return;
					}
					this.isCheckDxData = this.isCheckDxData.concat(this.isCheckDx)
				}
				this.dialog = false;
			}
		},
		onSearch(){
			this.pageIndex = 1
			this.getReportAbnormalPageAxios({abnormalName:this.abnormalName,pageIndex:this.pageIndex,pageSize:20})
		},
		// 获取所有体检项目
		getReportTemplateStencilPageAxios(data){
			uni.showLoading({title:'加载中...'})
			this.$api.getReportTemplateStencilPage(data).then((res)=>{
				uni.hideLoading()
				this.tiOption.totalCount = Math.ceil(res.data.result.totalCount / 20)
				this.dialogcontents = res.data.result.list.map((e)=>{
					e.name = e.itemName
					e.maxValue = e.maxVa
					e.minValue = e.minVa
					e.va = e.minVa+'~'+e.maxVa
					return e
				}).filter(b=>{
					if(this.sex == '男'){
						return b.itemName.indexOf('女') <= -1
					}else{
						return b.itemName.indexOf('男') <= -1
					}
				})
			})
		},
		// 获取所有异常
		getReportAbnormalPageAxios(data){
			uni.showLoading({title:'加载中...'})
			this.$api.getReportAbnormalPage(data).then((res)=>{
				uni.hideLoading()
				this.totalCount = Math.ceil(res.data.result.totalCount / 20)
				this.dialogcontents = res.data.result.list.map((e)=>{
					return {
						name:e.abnormalName,
						Wpercentage: '82vw',
						reportNo:e.abnormalCode
					}
				})
			})
		},
		getReportTemplateStencilAxios(){
			this.$api.getReportTemplateStencil().then((res)=>{
				this.beforeFilterData = res.data.result.map(a=>{
					a.itemId = a.reportNo;
					a.maxValue = a.maxVa
					a.minValue = a.minVa
					a.itemValue = ''
					return a;
				})
				// let _e = this.beforeFilterData.filter((ae)=>{
				// 	return ae.itemName.indexOf(this.sex == '男'?'女':'男') <= -1
				// })
				// this.beforeFilterData = _e;
				this.data.reportItemS = this.beforeFilterData;				
				this.filter(res.data.result)
				// this.getfindByMedicalAxios();
			})
		},
		// 数据分类转换
		filter(e){
			let _e = e.filter((ae)=>{
				return ae.itemName.indexOf(this.sex == '男'?'女':'男') <= -1
			})
			const data = this.sex ? _e : e
			let arr = []
			data.map((ev,i)=>{
				let hasIndex = arr.findIndex((av,ai)=>{  return av.sectionName == ev.sectionName})
				if(hasIndex > -1){
					if(ev.itemName == '血清高密度脂蛋白胆固醇') {
                        arr[hasIndex].contents.push({name:ev.itemName,units:ev.units,va:ev.maxVa + ' ~ ' + ev.reference,reportNo:ev.reportNo})
                    }else {
                        arr[hasIndex].contents.push({name:ev.itemName,units:ev.units,va:Number(ev.minVa) == 0 && Number(ev.maxVa) == 0 ? '': Number(ev.minVa).toFixed(2)+'~'+Number(ev.maxVa).toFixed(2),reportNo:ev.reportNo})
                    }
					arr[hasIndex].data.push(ev)
				}else{
					arr.push({data:[ev],sectionName:ev.sectionName,contents:[{name:ev.itemName,units:ev.units,va:Number(ev.minVa) == 0 && Number(ev.maxVa) == 0 ? '':  Number(ev.minVa).toFixed(2)+'~'+Number(ev.maxVa).toFixed(2),reportNo:ev.reportNo}]})
				}
			})
			this.defaultData = arr
		},
		getcallbackdata(e){
			let keyArr = Object.keys(e[0]).map((a)=>{
				return a
			})
			const reportItemS = this.beforeFilterData.map((a,ai)=>{
				e[1].map((c,ci)=>{
					if(c.value){
						if(c.reportNo == a.reportNo){
							a.itemValue = c.value
						}
					}
				})
				keyArr.map((b,bi)=>{
					if(a.reportNo == b){
						a.itemId = b;
						a.itemValue = e[0][b]
					}
				})
				return a
			})
			
			this.data.reportItemS = reportItemS
		},
		// 单项数据回调
		dxgetcallbackdata(e){
			let keyArr = Object.keys(e[0]).map((a)=>{
				return a
			})
			const reportItemS = e[1].map((a,ai)=>{
				keyArr.map((b,bi)=>{
					if(a.itemId == b){
						a.itemId = b;
						a.itemValue = e[0][b]
					}else if(a.reportNo == b){
						a.itemId = b;
						a.itemValue = e[0][b]
					}
				})
				return a
			})
			this.dxgetcallback = reportItemS
		},
		submit(e){
			const _this = this;
			if(!_this.userId){
				uni.showToast({
					title:'请选择会员！',
					icon:'none'
				})
				return;
			}
			if(!_this.start_date){
				uni.showToast({
					title:'请选择体检时间！',
					icon:'none'
				})
				return;
			}
			if(!_this.examinationOrgan){
				uni.showToast({
					title:'请填写体检机构！',
					icon:'none'
				})
				return;
			}
			let reportItemS = _this.dxgetcallback.length > 0?_this.data.reportItemS.concat(_this.dxgetcallback):_this.data.reportItemS.concat(_this.isCheckDxData)
			_this.data.reportItemS = reportItemS.filter((ae)=>{
				return ae.itemName.indexOf(_this.sex == '男'?'女':'男') <= -1
			})
			_this.data.reportConclusionS = _this.defaultData.map((e)=>{ return { checkTime: '' , sectionName: e.sectionName} })
			_this.data.reportAbnormalS = _this.isCheckYcData && _this.isCheckYcData.map((e)=>{
				return {
					abnormalName:e.name,
					reportNo:e.reportNo
				}
			})
			_this.data.reportItemS.map((a)=>{
				a.minVa = a.minVa+'' || '0.00'
				a.maxVa = a.maxVa+'' || '0.00'
				a.minValue = a.minValue+'' || '0.00'
				a.maxValue = a.maxValue+'' || '0.00'
			})
			_this.data.id = _this.userId
			_this.data.examinationOrgan = _this.examinationOrgan
			_this.data.reportDate = _this.start_date
			_this.data.medicalNo = getUrlParam('reportNo') || _this.medicalNo
			_this.data.status = e
			// 如果是从会员申请列表entrance = applist 进入保存
			if(getUrlParam('entrance')){
				_this.data.reportNumber = getUrlParam('reportNumber')
				_this.$api.reportAddReport(_this.data).then((res)=>{
					if(res.data.returnCode == 200){
						uni.showToast({title:'保存成功',icon:'none'})
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/quickly/apply/applylist'
							})
						},1000)
					}else{
						uni.showToast({title:res.data.message,icon:'none'})
					}
				})
			}else{
				if(!_this.editId){
					_this.$api.healthAddReport(_this.data).then((res)=>{
						if(res.data.returnCode == 200){
							uni.showToast({title:'保存成功',icon:'none'})
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/quickly/medicalReport/list'
								})
							},1000)
						}else{
							uni.showToast({title:res.data.message,icon:'none'})
						}
					})
				}else{
					// 如果是从会员申请列表entrance = applist 进入保存取reportNumber
					_this.data.reportNumber = getUrlParam('reportNo')
					_this.$api.reportupdate(_this.editId,_this.data).then((res)=>{
						if(res.data.returnCode == 200){
							uni.showToast({title:'保存成功',icon:'none'})
							setTimeout(()=>{
								uni.navigateTo({
									url:'/pages/quickly/medicalReport/list'
								})
							},1000)
						}else{
							uni.showToast({title:res.data.message,icon:'none'})
						}
					})
				}
			}
		},
		hidememberdialog(){
			this.memberdialog = false
		},
		// 选择会员
		chooseUser(e){
			this.sex = e.sex == 2 ? '女':'男',
			this.filter(this.beforeFilterData)
			this.userId = e.userId;
			this.userName = e.userName;
		},
		seveAdd(e){
			if(e==2){
				this.showModal = true
				this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改。'
				return;
			}
			this.submit(4)
		},
		Modalconfirm(){
			this.submit(2)
		},
		// 上传体检报告
		uploadreport(){
			const reportConclusionS = this.defaultData.map((e)=>{ return { checkTime: '' , sectionName: e.sectionName} })
			this.$api.uploadUserAssessReport({reportConclusionS:reportConclusionS,reportNumber:getUrlParam('reportNumber'),userId:getUrlParam('userId')}).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({title:'保存成功',icon:'none'})
					setTimeout(()=>{
						uni.navigateTo({
							url:'/pages/quickly/apply/applylist'
						})
					},1000)
				}else{
					uni.showToast({title:res.data.message,icon:'none'})
				}
			})
		}
	}
}

